<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="hongbao" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主体部分 -->
        <el-tree default-expand-all :data="list">
          <!-- 不是每个作用域插槽都是 row, 具体是什么得根据实际情况打印查看 -->
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hShowEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 弹出框 -->
    <el-dialog
      :title="isEdit ? '编辑' : '添加'"
      :visible.sync="showDialog"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <add-or-edit v-if="showDialog" :id="curId" :is-edit="isEdit" @success="hSuccess" />
    </el-dialog>
  </div>

</template>
<script>
import { getliebiao, deleteDepartmentById } from '@/api/zuzhiliebiao'
import { tranListToTreeData } from '@/utils/index'
import addOrEdit from './deptDialog.vue'

export default {
  name: 'Departments',
  components: { addOrEdit },
  data() {
    return {
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false
    }
  },
  created() {
    this.huoquliebiao()
  },
  methods: {
    // 删除部门
    hDel(id) {
      this.$confirm('真的要删除吗?嘤嘤嘤!', '提示', {
        type: 'warning'
      })
        .then(async() => {
          try {
            // 发请求
            const res = await deleteDepartmentById(id)
            // 根据结果提醒用户
            this.$message.success(res.message)
            // 重新渲染页面
            this.huoquliebiao()
          } catch (e) {
            this.$message.error(e.message)
          }
        })
        .catch(() => {})
    },
    // 编辑部门
    hShowEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
    },
    // 子组件关闭弹窗
    hSuccess() {
      this.showDialog = false
      this.huoquliebiao()
    },
    // 增加部门
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    // 获取组织列表
    async huoquliebiao() {
      const res = await getliebiao()
      console.log(res)
      this.list = tranListToTreeData(res.data.depts)
    }
  }
}
</script>
